package com.xhwl.logistics.service.feign.impl;

import cn.hutool.core.date.DateUtil;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.query.cent.vehicle.PayRecordQuery;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.dto.http.InComeReportInfoKeTopDTO;
import com.xhwl.logistics.dto.http.InComeReportKeTopDTO;
import com.xhwl.logistics.dto.http.query.GetInComeReportKeTopDTO;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.service.feign.IVehiclePaymentRecord;
import com.xhwl.logistics.service.impl.http.KeTopHttpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author masiyuan
 * @since 2023年05月25日 14:28
 */
//@Service
public class IVehiclePaymentRecordImpl implements IVehiclePaymentRecord {


    @Autowired
    private KeTopHttpServiceImpl keTopHttpService;

    @Autowired
    private QueryDeviceService queryDeviceService;



    @Override
    public ResultJson queryInComeReport(PayRecordQuery query) {

        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(query.getProjectId());

        GetInComeReportKeTopDTO getInComeReportKeTopDTO = new GetInComeReportKeTopDTO();
        getInComeReportKeTopDTO.setStartTime(DateUtil.formatDate(query.getStartTime()));
        getInComeReportKeTopDTO.setEndTime(DateUtil.formatDate(query.getEndTime()));
        getInComeReportKeTopDTO.setPageIndex(1);
        getInComeReportKeTopDTO.setPageSize(500);
        InComeReportKeTopDTO inComeReportKeTopDTO = keTopHttpService.getInComeReport(vehicleIntegratedConfigBO, getInComeReportKeTopDTO);

        Map<String, Object> map = new HashMap<>();
        if(ChkUtil.isNotNull(inComeReportKeTopDTO.getStatList())) {
            InComeReportInfoKeTopDTO inComeReportInfoKeTopDTO = inComeReportKeTopDTO.getStatList().get(0);
            // 总金额
            BigDecimal totalFee = new BigDecimal(inComeReportInfoKeTopDTO.getTotalFee());
            map.put("totalFee", totalFee);
            // 临停缴费金额
            BigDecimal parkingFee = new BigDecimal(inComeReportInfoKeTopDTO.getParkingFee());
            map.put("parkingFee", parkingFee);
            // 月卡缴费金额
            BigDecimal carRechargeFee = new BigDecimal(inComeReportInfoKeTopDTO.getCarRechargeFee());
            map.put("carRechargeFee", carRechargeFee);
            // 月卡缴费占比
            Double carRechargeFeeRatio = carRechargeFee.divide(totalFee, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
            map.put("carRechargeFeeRatio", carRechargeFeeRatio);
            // 临停车缴费占比
            Double parkingFeeRatio = parkingFee.divide(totalFee, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
            map.put("parkingFeeRatio", parkingFeeRatio);
        }

        return ResultJson.success(map);
    }
}
